package com.zxl.live.screen.ui.widget;

import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * Created by Administrator on 2018/2/11.
 */

public class GrildRecyclerDecoration extends RecyclerView.ItemDecoration {

    private int spacing;

    private int spanCount;

    private boolean includeEdge;

    public GrildRecyclerDecoration(int space, int spanCount, boolean includeEdge) {
        this.spacing = space;
        this.spanCount = spanCount;
        this.includeEdge = includeEdge;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        int position = parent.getChildAdapterPosition(view); // item position

        int column = position % spanCount; // item column
        if (includeEdge) {
            if (position == 0) {
                return;
            }
            outRect.left = (int) (spacing - column * ((1f / spanCount) * spacing)); //spacing - column * spacing / spanCount;
            outRect.right = (int) ((column + 1) * ((1f / spanCount) * spacing));  //(column + 1) * spacing / spanCount;
            outRect.top = spacing;
            outRect.bottom = spacing; // item bottom
        } else {
            outRect.left = column * spacing / spanCount; // column * ((1f / spanCount) * spacing)
            outRect.right = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f /    spanCount) * spacing)
            if (position >= spanCount) {
                outRect.top = spacing; // item top
            }
        }
    }
}
